package com.CTAtracker.android.Adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.DAO.StationDataSource;
import com.CTAtracker.android.Models.Station;

public class SearchAdapter extends ArrayAdapter<Station> {
	/**
	 * The layout to inflate 
	 */
	private LayoutInflater _layoutInflater = null;
	/**
	 * The id of the resource 
	 */
	private int _resourceId = 0;
	/**
	 * The array list to adapt to the view
	 */
	private ArrayList<Station> _listStations;
	/**
	 * Link to the database of Station
	 */
	private StationDataSource _stationDataSource;
	
	public SearchAdapter(Activity activity, int idResource,
			ArrayList<Station> listStation) {
		super(activity, idResource, 0, listStation);
		_layoutInflater = LayoutInflater.from(activity);
		_resourceId = idResource;
		_listStations = listStation;
		_stationDataSource = new StationDataSource(activity);
	}
	
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		Station station = _listStations.get(position);
		convertView = _layoutInflater.inflate(_resourceId, parent, false);

		FrameLayout fl = (FrameLayout) convertView.findViewById(R.id.searchFrameColor);
		TextView tvn = (TextView) convertView.findViewById(R.id.searchNameStation);
		
		tvn.setText(station.get_name());
		fl.setBackgroundColor(Color.parseColor(station.get_color().toString()));

		return convertView;
	}

	public void updateListView(String string) {
		
		if (string.length()>=1){
			_stationDataSource.open(); 
			clear(); 
			addAll(_stationDataSource.getStationMatches(string));
			_stationDataSource.close();
			this.notifyDataSetChanged();
		}
	}

}
